package com.chb.demo.controller;

import com.chb.demo.entity.Admin;
import com.chb.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping("/getadmins")
    @ResponseBody  // 把返回的结果json序列化
    public Object getAdmins(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize) {
        Page<Admin> page = adminService.findAllAdmin(pageNo, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "ok");
        map.put("count", page.getTotalElements());
        map.put("data", page.getContent());
        return map;//hello.html..
    }

    @RequestMapping("/addAdmin")
    @ResponseBody  // 把返回的结果json序列化
    public Object addAdmin(@RequestParam("account") String account
            , @RequestParam("password") String password
            , @RequestParam("displayname") String displayname
            , @RequestParam("id") String id
            , @RequestParam("phone") String phone) {
        System.out.println(account+password);
        Map<String, Object> map = new HashMap<>();
        map.put("result", adminService.saveAdmin(account, password, displayname, id, phone));
        if (map.get("result").equals("插入成功")){
            map.put("success",true);
        }
        else {
            map.put("success",false);
        }

        return map;
    }

    @RequestMapping("/batchDeleteAdmin")
    @ResponseBody  // 把返回的结果json序列化
    public Object batchDeleteAdmin(@RequestParam("id") List<String> accounts) {
        System.out.println(accounts.toString());
        boolean success = adminService.bathDelete(accounts);
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);
        return map;
    }

    @RequestMapping("/oneDeleteAdmin")
    @ResponseBody  // 把返回的结果json序列化
    public Object batchDeleteAdmin(@RequestParam("id") String account) {
        System.out.println(account);
        boolean success = adminService.oneDelete(account);
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);
        return map;
    }

    @RequestMapping("/updateAdmin")
    @ResponseBody  // 把返回的结果json序列化
    public Object updateAdmin(@RequestParam("account") String account
            , @RequestParam("password") String password
            , @RequestParam("displayname") String displayname
            , @RequestParam("id") String id
            , @RequestParam("phone") String phone) {
        System.out.println(account);
        boolean success = adminService.updateAdmin(account, password, displayname, id, phone);
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);
        return map;
    }
}
